//
//  OnlineMenuView.m
//  Molian
//
//  Created by 小蚕 on 15/9/4.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "OnlineMenuView.h"
#define kMyBtTag 156

@interface OnlineMenuView ()
{
    UIView *bgView;
    UIView *lineView;
    UIView *lineView2;
    UIButton *leftBt;
    UIButton *rightBt;
}
@property (nonatomic, strong)NSArray *titleArr;
@property (nonatomic, strong)NSArray *imageTitle;
@end

@implementation OnlineMenuView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        self.backgroundColor = RGBA(1, 1, 1, 0.3);
        
        
        
        bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, 270)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        
        UIView *dismissView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight+270, DeviceWidth, DeviceHeight-kNavigationBarHeight-270)];
        dismissView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBuyView)];
        [dismissView addGestureRecognizer:tap];
        [self addSubview:dismissView];
        NSDictionary *dic = [Utils screenRecord];
        if (dic) {
            self.gender = [[dic objectForKey:@"sex"] integerValue];
            self.userType = [[dic objectForKey:@"userType"] integerValue];//筛选普通用户
        }else{
            self.gender = 0;
            self.userType = 0;//筛选普通用户
        }
        UIColor *labelColor = RGBA(135, 135, 135, 1);
        UIColor *btBackgroundColor = RGBA(242, 242, 242, 1);
        UIColor *btTextColor = RGBA(76, 78, 70, 1);
    
        NSArray *arr = [NSArray arrayWithObjects:@"全部",@"男生",@"女生",@"梦想恋人",@"专属恋人",@"实习恋人", nil];
        
        for (int i = 0; i<2; i++) {
            UILabel *genderlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8+i*76, 150, 15)];
            genderlabel.textColor = labelColor;
            genderlabel.font = kFont(14);
            if (i == 0) {
                 genderlabel.text = @"性别";
            }else{
                genderlabel.text = @"恋人类型";
            }
           
            [bgView addSubview:genderlabel];
            
            UIView *btBGView = [[UIView alloc] initWithFrame:CGRectMake(10, 32.5+i*(105-32.5), DeviceWidth-20, 44)];
            CGFloat h = btBGView.frame.size.height;
            CGFloat w = btBGView.frame.size.width;
            btBGView.backgroundColor = btBackgroundColor;
            btBGView.layer.masksToBounds = YES;
            btBGView.layer.cornerRadius = 5;
            [bgView addSubview:btBGView];
            for (int j = 0; j<3; j++) {
                UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
                bt.frame = CGRectMake(w/3/2-80/2+j*w/3, h/2-28/2, 80, 28);
                bt.layer.masksToBounds = YES;
                bt.layer.cornerRadius = 4;
                [bt setTitle:arr[i*3+j] forState:UIControlStateNormal];
                bt.tag = kMyBtTag +i*3+j;
                bt.titleLabel.font = kFont(16);
                if (i == 0&&j == self.gender ) {
                 [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    bt.backgroundColor = APPCOLOR(1);
                }else if(i == 1){
                    if (self.userType == 2&&j == 0) {
                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                        bt.backgroundColor = APPCOLOR(1);
                    }else if(self.userType == 1&&j == 1){
                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                        bt.backgroundColor = APPCOLOR(1);
                    }else if(self.userType == 0&&j == 2){
                        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                        bt.backgroundColor = APPCOLOR(1);
                    }else{
                         [bt setTitleColor:btTextColor forState:UIControlStateNormal];
                        bt.backgroundColor = [UIColor clearColor];
                    }
                }else{
                     [bt setTitleColor:btTextColor forState:UIControlStateNormal];
                    bt.backgroundColor = [UIColor clearColor];
                }
                [bt addTarget:self action:@selector(clickBt:) forControlEvents:UIControlEventTouchUpInside];
                [btBGView addSubview:bt];
            }
        }
        
        

        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(10, 164, DeviceWidth - 20, 32)];
        myView.backgroundColor = [UIColor clearColor];
        [bgView addSubview:myView];
        CGFloat h = myView.frame.size.width;
        for (int i = 0; i<2; i++) {
            for (int j = 0; j<3; j++) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((h/3-100)/2+j*h/3, i*(24), 16, 16)];
                imageView.hidden = YES;
                imageView.tag = kMyBtTag +10+i*3+j;
                [myView addSubview:imageView];
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x + 16+ 10, imageView.frame.origin.y, 74, 16)];
                label.textColor = labelColor;
                label.hidden = YES;
                label.tag = kMyBtTag +20+i*3+j;
                label.font = kFont(14);
                [myView addSubview:label];
                
            }
        }
        [self changeTitleArrAndImageArr];
        
        
        lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height - 50.5, DeviceWidth, 0.5)];
        lineView.backgroundColor = RGBA(200, 200, 200, 1);
        [bgView addSubview:lineView];
        
        lineView2 = [[UIView alloc] initWithFrame:CGRectMake(DeviceWidth/2-0.25, bgView.frame.size.height - 50.5+10, 0.5, 30)];
        lineView2.backgroundColor = RGBA(200, 200, 200, 1);
        [bgView addSubview:lineView2];
        
        leftBt = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBt.frame = CGRectMake(0, bgView.frame.size.height-50, DeviceWidth/2, 50);
        [leftBt setTitle:@"取消" forState:UIControlStateNormal];
        [leftBt addTarget:self action:@selector(clickBuyViewLeftBt) forControlEvents:UIControlEventTouchUpInside];
        leftBt.titleLabel.font = kFont(15);
        [leftBt setTitleColor:RGBA(33, 33, 33, 1) forState:UIControlStateNormal];
        [leftBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
        [bgView addSubview:leftBt];
        
        rightBt = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBt.frame = CGRectMake(DeviceWidth/2, bgView.frame.size.height-50, DeviceWidth/2, 50);
        [rightBt setTitle:@"确定" forState:UIControlStateNormal];
        [rightBt addTarget:self action:@selector(clickBuyViewRightBt) forControlEvents:UIControlEventTouchUpInside];
        rightBt.titleLabel.font = kFont(15);
        [rightBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
        [rightBt setTitleColor:RGBA(188, 188, 188, 1) forState:UIControlStateHighlighted];
        [bgView addSubview:rightBt];
        
    }
    return self;
}
- (void)changeTitleArrAndImageArr
{
    CGFloat h = 0;
    if (self.userType == 0) {
        h = 30;
        
        self.titleArr = [NSArray arrayWithObjects:@"注册用户",nil];
        self.imageTitle = [NSArray arrayWithObjects:@"注册用户",nil];
    }else if(self.userType == 1){
        h = 0;
        self.titleArr = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"200时光/天",nil];
        self.imageTitle = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"时光-天",nil];
    }else{
        h = 0;
        self.titleArr = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"300时光/天",@"恋人故事",nil];
        self.imageTitle = [NSArray arrayWithObjects:@"真人认证", @"出租时间",@"1v1陪伴",@"高颜值",@"时光-天",@"恋人故事",nil];
    }
    bgView.frame = CGRectMake(0, kNavigationBarHeight, DeviceWidth, 270-h);
    lineView.frame = CGRectMake(0, bgView.frame.size.height - 50.5, DeviceWidth, 0.5);
    lineView2.frame = CGRectMake(DeviceWidth/2-0.25, bgView.frame.size.height - 50.5+10, 0.5, 30);
    
    leftBt.frame = CGRectMake(0, bgView.frame.size.height-50, DeviceWidth/2, 50);
    
    rightBt.frame = CGRectMake(DeviceWidth/2, bgView.frame.size.height-50, DeviceWidth/2, 50);
    [self changeTitleAndImage];
}
- (void)changeTitleAndImage
{
    
    for (int i = 0; i<6; i++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:kMyBtTag+10+i];
        UILabel *label = (UILabel *)[self viewWithTag:kMyBtTag+20+i];
        if (i<self.titleArr.count) {
            imageView.hidden = NO;
            label.hidden = NO;
            
            if(i == 4){
                NSString *str = self.titleArr[i];
                NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:str];
                //[mString addAttribute:NSForegroundColorAttributeName value:APPCOLOR(1) range:NSMakeRange(str.length-4,4)];
                [mString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:10] range:NSMakeRange(str.length-4,4)];
                label.attributedText = mString ;
            }else{
                label.text = self.titleArr[i];
            }
            
            
            
            imageView.image = [UIImage imageNamed:self.imageTitle[i]];
        }else{
            imageView.hidden = YES;
            label.hidden = YES;
        }
    }
}
- (void)clickBuyView
{
    if (self.alpha == 0) {
        [Utils Hidden:self Bool:YES];
    }else{
        [Utils Hidden:self Bool:NO];
    }
}
- (void)clickBuyViewLeftBt
{
    [Utils Hidden:self Bool:NO];
}
- (void)clickBuyViewRightBt
{

    [Utils screenRecordSex:self.gender userType:self.userType];
    if ([self.delegate respondsToSelector:@selector(clickListButton)]) {
        [self.delegate clickListButton];
    }
    
    [Utils Hidden:self Bool:NO];
}
- (void)clickBt:(UIButton *)button
{
    UIButton *bt = (UIButton *)[self viewWithTag:kMyBtTag];
    UIButton *bt1 = (UIButton *)[self viewWithTag:kMyBtTag+1];
    UIButton *bt2 = (UIButton *)[self viewWithTag:kMyBtTag+2];
    UIButton *bt3 = (UIButton *)[self viewWithTag:kMyBtTag+3];
    UIButton *bt4 = (UIButton *)[self viewWithTag:kMyBtTag+4];
    UIButton *bt5 = (UIButton *)[self viewWithTag:kMyBtTag+5];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = APPCOLOR(1);
     UIColor *btTextColor = RGBA(76, 78, 70, 1);
    switch (button.tag) {
        case kMyBtTag:
        {
            self.gender = 0;
            [bt1 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt1.backgroundColor = [UIColor clearColor];
            [bt2 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt2.backgroundColor = [UIColor clearColor];
            
        }
            
            //全部
            break;
        case kMyBtTag+1:
            self.gender = 1;
            //男生
            [bt setTitleColor:btTextColor forState:UIControlStateNormal];
            bt.backgroundColor = [UIColor clearColor];
            [bt2 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt2.backgroundColor = [UIColor clearColor];
            break;
        case kMyBtTag+2:
            //女生
            self.gender = 2;
            [bt1 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt1.backgroundColor = [UIColor clearColor];
            [bt setTitleColor:btTextColor forState:UIControlStateNormal];
            bt.backgroundColor = [UIColor clearColor];
            break;
        case kMyBtTag+3:
            self.userType = 2;
           
            //梦想恋人
            [bt4 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt4.backgroundColor = [UIColor clearColor];
            [bt5 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt5.backgroundColor = [UIColor clearColor];
            break;
        case kMyBtTag+4:
            self.userType = 1;
            
            //专属恋人
            [bt3 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt3.backgroundColor = [UIColor clearColor];
            [bt5 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt5.backgroundColor = [UIColor clearColor];
            break;
        case kMyBtTag+5:
            self.userType = 0;
           
            //实习恋人
            [bt4 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt4.backgroundColor = [UIColor clearColor];
            [bt3 setTitleColor:btTextColor forState:UIControlStateNormal];
            bt3.backgroundColor = [UIColor clearColor];
            break;
       
            
        default:
            break;
    }
    [self changeTitleArrAndImageArr];
}
@end
